<template>
  <div>
    <span>全选:</span>
    <!-- 4. v-model 关联全选 - 选中状态 -->
    <input type="checkbox" v-model="isAll"/>
    <button @click="disselect">反选</button>
    <ul>
      <li v-for="(obj, index) in arr" :key="index">
        <!-- 3. 对象.c - 关联 选中状态 -->
        <input type="checkbox" v-model="obj.c"/>
        <span>{{ obj.name }}</span>
      </li>
    </ul>
  </div>
</template>

<script>
// 目标: 小选框 -> 全选
// 1. 标签+样式+js准备好
// 2. 把数据循环展示到页面上
export default {
  data () {
    return {
      arr: [
        {
          name: '猪八戒',
          c: false
        },
        {
          name: '孙悟空',
          c: false
        },
        {
          name: '唐僧',
          c: false
        },
        {
          name: '白龙马',
          c: false
        }
      ]
    }
  },
  // 5. 计算属性-isAll
  computed: {
    isAll: {
      get () {
        return this.arr.every(obj => obj.c === true)
      },
      set (val) {
        this.arr.forEach(item => (item.c = val))
      }
    }
  },
  methods: {
    disselect () {
      this.arr.forEach(item => { item.c = !item.c })
    }
  }
}
</script>
